package my.georings;


import my.georings.data.Settings;
import my.georings.service.LocationChangeService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemSelectedListener;



public class SettingsActivity extends Activity implements OnClickListener, OnItemSelectedListener{

	private boolean isRunning = false;
	private LocationChangeService boundService;
	private Settings sets = new Settings();
	private Settings changed = new Settings();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.settings);
        
        findViewById(R.id.settings_start).setOnClickListener(this);
        findViewById(R.id.settings_stop).setOnClickListener(this);
        
        findViewById(R.id.ToggleButtonGPS).setOnClickListener(this);
        findViewById(R.id.ToggleButtonReduce).setOnClickListener(this);
        findViewById(R.id.ToggleButtonAuto).setOnClickListener(this);
        
        initSpinner();
        initSettings();
        
        if(isRunning = ((GeoPhoneRingsApp)getApplication()).checkServiceRunning()){
        	findViewById(R.id.settings_start).setEnabled(false);
        }else{
        	findViewById(R.id.settings_stop).setEnabled(false);
        }
    }


	@Override
	public void onClick(View v) {

		boolean change = false;
		if(v.getId() == R.id.ToggleButtonGPS){
			changed.setGps( ((ToggleButton)findViewById(R.id.ToggleButtonGPS)).isChecked() );
			change = true;
		}
		else if(v.getId() == R.id.ToggleButtonReduce){
			changed.setReduceInterval(((ToggleButton)findViewById(R.id.ToggleButtonReduce)).isChecked() );
			change = true;
		}
		
		if(change && isRunning){
			doBindService();
			return;
		}else if(change){
			sets.copyFrom(changed);
			return;
		}
		
		if(v.getId() == R.id.ToggleButtonAuto){
			sets.setStopAutomatically(((ToggleButton)findViewById(R.id.ToggleButtonAuto)).isChecked() );
			return;
		}
		
		// service
		if(v.getId() == R.id.settings_start){
			isRunning = true;
			
			Intent svc = new Intent(this, LocationChangeService.class);
			
		    startService(svc);
		    findViewById(R.id.settings_start).setEnabled(false);
		    findViewById(R.id.settings_stop).setEnabled(true);
		    
		    // przekazanie odpowiednich ustawien do serwisu
		    doBindService();
		}
		else if(v.getId() == R.id.settings_stop){
			isRunning = false;
			
			Intent svc = new Intent(this, LocationChangeService.class);
		    
			stopService(svc);
		    findViewById(R.id.settings_stop).setEnabled(false);
		    findViewById(R.id.settings_start).setEnabled(true);
		}
	}
	
	
	
	private void initSpinner(){
		Spinner spinner = (Spinner) findViewById(R.id.settings_spinner);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.intervals, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(this);
	}
	
	
	private void initSettings(){
		sets = ((GeoPhoneRingsApp)getApplication()).getSettings();
		changed.copyFrom(sets);
		
		((ToggleButton)findViewById(R.id.ToggleButtonGPS)).setChecked(sets.isGps());
		((ToggleButton)findViewById(R.id.ToggleButtonReduce)).setChecked(sets.isReduceInterval());
		((ToggleButton)findViewById(R.id.ToggleButtonAuto)).setChecked(sets.isStopAutomatically());
		
		((Spinner)findViewById(R.id.settings_spinner)).setSelection(sets.getIntervalIndex());
		
		LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		((ToggleButton)findViewById(R.id.ToggleButtonGPS)).setEnabled(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
	}
	

	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int index, long theid) {
		changed.setIntervalIndex(index);
		if(isRunning)
			doBindService();
		else
			sets.copyFrom(changed);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {}
	
	
	void doBindService() {
		Intent myIntent = new Intent(this.getParent(), my.georings.service.LocationChangeService.class);
		this.getParent().bindService(myIntent, boundConnection, Context.BIND_AUTO_CREATE);
	}

	void doUnbindService() {
	    if (boundService != null) {
	    	this.getParent().unbindService(boundConnection);
	        boundService = null;
	    }
	}

	@Override
	protected void onDestroy() {
	    super.onDestroy();
	    doUnbindService();
	}
	
	
	
	private ServiceConnection boundConnection = new ServiceConnection() {
	    public void onServiceConnected(ComponentName className, IBinder service) {
	    	boundService = ((LocationChangeService.LocationChangeBinder)service).getService();
	    	
	    	// tu zmiana ustawien tych, ktore sie zmienily
	    	if(changed.isGps() != sets.isGps())
	    		boundService.changeUseGPS(changed.isGps());
	    	
	    	if(changed.getInterval() != sets.getInterval())
	    		boundService.changeInterval(changed.getInterval());
	    	
	    	if(changed.isReduceInterval() != sets.isReduceInterval())
	    		boundService.changeAdjust(changed.isReduceInterval());
	    	
	    	sets.copyFrom(changed);
	    	
	    	if(SettingsActivity.this.getParent() instanceof MainActivity){
	    		MainActivity main = (MainActivity) SettingsActivity.this.getParent();
	    		main.setStateListener(boundService);
	    	}
	    	
	    	doUnbindService();
	    }

	    public void onServiceDisconnected(ComponentName className) {
	    	boundService = null;
	    }
	};


}
